package org.biubiu0002.baiblog.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.biubiu0002.baiblog.common.constants.CacheConstants;
import org.biubiu0002.baiblog.common.utils.PageUtils;
import org.biubiu0002.baiblog.common.utils.Query;
import org.biubiu0002.baiblog.core.dao.FavorRecordDao;
import org.biubiu0002.baiblog.core.model.entity.FavorRecordEntity;
import org.biubiu0002.baiblog.core.service.FavorRecordService;
import org.biubiu0002.baiblog.core.service.ShareFavorCountService;
import org.biubiu0002.baiblog.core.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service("favorRecordService")
public class FavorRecordServiceImpl extends ServiceImpl<FavorRecordDao, FavorRecordEntity> implements FavorRecordService {


    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    ShareFavorCountService shareFavorCountService;

    @Resource
    FavorRecordDao favorRecordDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FavorRecordEntity> page = this.page(
                new Query<FavorRecordEntity>().getPage(params),
                new QueryWrapper<FavorRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void favorShare(String userUid, String shareId) {

        //持久化收藏关系
        FavorRecordEntity favorRecordEntity = new FavorRecordEntity();
        favorRecordEntity.setUserUid(userUid);
        favorRecordEntity.setShareId(shareId);
        favorRecordEntity.setIsFavor(true);
        favorRecordDao.saveOrUpdate(favorRecordEntity);


        //记录收藏情况set
        redisTemplate.opsForSet().add(CacheConstants.getShareUserFavorKey(userUid), shareId);

    }

    @Override
    public void unFavorShare(String userUid, String shareId) {

        //判断收藏情况 只有收藏过才取消收藏 直接查数据库有没有这条记录
        FavorRecordEntity favorRecordEntity = this.getByUserUidShareId(userUid, shareId);
        if (favorRecordEntity == null) {
            return;
        }
        //持久化收藏关系
        favorRecordEntity = new FavorRecordEntity();
        favorRecordEntity.setUserUid(userUid);
        favorRecordEntity.setShareId(shareId);
        favorRecordEntity.setIsFavor(false);
        favorRecordDao.saveOrUpdate(favorRecordEntity);

        //redis删除
        redisTemplate.opsForSet().remove(CacheConstants.getShareUserFavorKey(userUid),shareId);


    }

    @Override
    public void loadFromDB2Redis(String userUid) {
        List<FavorRecordEntity> favorRecordEntityList = this.getByUserUid(userUid);
        if (CollectionUtils.isEmpty(favorRecordEntityList)) {
            return;
        }
        redisTemplate.opsForSet().add(CacheConstants.getShareUserFavorKey(userUid),
                favorRecordEntityList.stream().map(FavorRecordEntity::getShareId).toArray(String[]::new));
        redisTemplate.expire(CacheConstants.getShareUserFavorKey(userUid)
                , CacheConstants.getShareUserFavorExpireMinutes(), TimeUnit.MINUTES);
    }


    @Override
    public List<FavorRecordEntity> getByUserUid(String userUid) {
        return baseMapper.selectList(new QueryWrapper<FavorRecordEntity>()
                .eq("user_uid", userUid)
                .eq("is_favor", 1)
                .eq("is_deleted", 0)
        );
    }

    @Override
    public FavorRecordEntity getByUserUidShareId(String userUid, String shareId) {
        return baseMapper.selectOne(new QueryWrapper<FavorRecordEntity>()
                .eq("user_uid", userUid)
                .eq("share_id", shareId)
                .eq("is_deleted", 0)
        );
    }

    @Override
    public Boolean isUserFavorShare(String userUid, String shareId) {
        Boolean aBoolean = redisTemplate.hasKey(CacheConstants.getShareUserFavorKey(userUid));
        if (Boolean.FALSE.equals(aBoolean)) {
            this.loadFromDB2Redis(userUid);
        }
        return redisTemplate.opsForSet().isMember(CacheConstants.getShareUserFavorKey(userUid), shareId);
    }

    public void deleteFavorRecord(FavorRecordEntity favorRecordEntity) {
        FavorRecordEntity favorRecordEntity1 = new FavorRecordEntity();
        favorRecordEntity1.setIsDeleted(1);
        baseMapper.update(favorRecordEntity1, new UpdateWrapper<FavorRecordEntity>()
                .eq("user_uid", favorRecordEntity.getUserUid())
                .eq("share_id", favorRecordEntity.getShareId())
        );
    }


}